1 <?php
2 include("../includes/config.php");
3 include("../includes/validate_data.php");
4 session_start();
5 if(isset($_SESSION['admin_login'])) {
6 if($_SESSION['admin_login'] == true) {
7 $query_selectCategory = "SELECT cat_id,cat_name FROM categories";
8 $query_selectUnit = "SELECT id,unit_name FROM unit";
9 $result_selectCategory = mysqli_query($con,$query_selectCategory);
10 $result_selectUnit = mysqli_query($con,$query_selectUnit);
11 $name = $price = $unit = $category = $rdbStock = $description = "";
12 $nameErr = $priceErr = $requireErr = $confirmMessage = "";
13 $nameHolder = $priceHolder = $descriptionHolder = "";
14 if($_SERVER['REQUEST_METHOD'] == "POST") {
15 if(!empty($_POST['txtProductName'])) {
16 $nameHolder = $_POST['txtProductName'];
17 $name = $_POST['txtProductName'];
18 }
19 if(!empty($_POST['txtProductPrice'])) {
20 $priceHolder = $_POST['txtProductPrice'];
21 $resultValidate_price = validate_price($_POST['txtProductPrice']);
22 if($resultValidate_price == 1) {
23 $price = $_POST['txtProductPrice'];
24 }
25 else {
26 $priceErr = $resultValidate_price;
27 }
28 }
29 if(isset($_POST['cmbProductUnit'])) {
30 $unit = $_POST['cmbProductUnit'];
31 }
32 if(isset($_POST['cmbProductCategory'])) {
33 $category = $_POST['cmbProductCategory'];
34 }
35 if(empty($_POST['rdbStock'])) {
36 $rdbStock = "";
37 }
38 else {
39 if($_POST['rdbStock'] == 1) {
40 $rdbStock = 1;
41 }
42 else if($_POST['rdbStock'] == 2) {
43 $rdbStock = 2;
44 }
45 }
46 if(!empty($_POST['txtProductDescription'])) {
47 $description = $_POST['txtProductDescription'];
48 $descriptionHolder = $_POST['txtProductDescription'];
49 }
50 if($name != null && $price != null && $unit != null && $category != null && $rdbStock == 1) {
51 $rdbStock = 0;
52 $query_addProduct = "INSERT INTO products(pro_name,pro_desc,pro_price,unit,pro_cat,quantity) VALUES('$name','$description','$price','$unit','$category','$rdbStock')";
53 if(mysqli_query($con,$query_addProduct)) {
54 echo "<script> alert(\"Product Added Successfully\"); </script>";
55 header('Refresh:0');
56 }
57 else {
58 $requireErr = "Adding Product Failed";
59 }
60 }
61 else if($name != null && $price != null && $unit != null && $category != null && $rdbStock == 2) {
62 $query_addProduct = "INSERT INTO products(pro_name,pro_desc,pro_price,unit,pro_cat,quantity) VALUES('$name','$description','$price','$unit','$category',NULL)";
63 if(mysqli_query($con,$query_addProduct)) {
64 echo "<script> alert(\"Product Added Successfully\"); </script>";
65 header('Refresh:0');
66 }
67 else {
68 $requireErr = "Adding Product Failed";
69 }
70 }
71 else {
72 $requireErr = "* All Fields are Compulsory with valid values except Description";
73 }
74 }
75 }
76 else {
77 header('Location:../index.php');
78 }
79 }
80 else {
81 header('Location:../index.php');
82 }
83 ?>
84 <!DOCTYPE html>
85 <html>
86 <head>
87 <title> Add Product </title>
88 <link rel="stylesheet" href="../includes/main_style.css" >
89 </head>
90 <body>
91 <?php
92 include("../includes/header.inc.php");
93 include("../includes/nav_admin.inc.php");
94 include("../includes/aside_admin.inc.php");
95 ?>
96 <section>
97 <h1>Add Product</h1>
98 <form action="" method="POST" class="form">
99 <ul class="form-list">
100 <li>
101 <div class="label-block"> <label for="product:name">Product Name</label> </div>
102 <div class="input-box"> <input type="text" id="product:name" name="txtProductName" placeholder="Product Name" value="<?php echo $nameHolder; ?>" required /> </div> <span class="error_message"><?php echo $nameErr; ?></span>
103 </li>
104 <li>
105 <div class="label-block"> <label for="product:price">Price</label> </div>
106 <div class="input-box"> <input type="text" id="product:price" name="txtProductPrice" placeholder="Price" value="<?php echo $priceHolder; ?>" required /> </div> <span class="error_message"><?php echo $priceErr; ?></span>
107 </li>
108 <li>
109 <div class="label-block"> <label for="product:unit">Unit Type</label> </div>
110 <div class="input-box">
111 <select name="cmbProductUnit" id="product:unit">
112 <option value="" disabled selected>--- Select Unit ---</option>
113 <?php while($row_selectUnit = mysqli_fetch_array($result_selectUnit)) { ?>
114 <option value="<?php echo $row_selectUnit["id"]; ?>"> <?php echo $row_selectUnit["unit_name"]; ?> </option>
115 <?php } ?>
116 </select>
117 </div>
118 </li>
119 <li>
120 <div class="label-block"> <label for="product:category">Category</label> </div>
121 <div class="input-box">
122 <select name="cmbProductCategory" id="product:category">
123 <option value="" disabled selected>--- Select Category ---</option>
124 <?php while($row_selectCategory = mysqli_fetch_array($result_selectCategory)) { ?>
125 <option value="<?php echo $row_selectCategory["cat_id"]; ?>"> <?php echo $row_selectCategory["cat_name"]; ?> </option>
126 <?php } ?>
127 </select>
128 </div>
129 </li>
130 <li>
131 <div class="label-block"> <label for="product:stock">Stock Management</label> </div>
132 <input type="radio" name="rdbStock" value="1">Enable
133 <input type="radio" name="rdbStock" value="2">Disable
134 </li>
135 <li>
136 <div class="label-block"> <label for="product:description">Description</label> </div>
137 <div class="input-box"> <textarea type="text" id="product:description" name="txtProductDescription" placeholder="Description"><?php echo $descriptionHolder; ?></textarea> </div>
138 </li>
139 <li>
140 <input type="submit" value="Add Product" class="submit_button" /> <span class="error_message"> <?php echo $requireErr; ?> </span><span class="confirm_message"> <?php echo $confirmMessage; ?> </span>
141 </li>
142 </ul>
143 </form>
144 </section>
145 <?php
146 include("../includes/footer.inc.php");
147 ?>
148 </body>
149 </html>